package com.deyuanyun.pic.ctrl.validatefun;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.ctrl.annotation.DbTable;
import com.deyuanyun.pic.ctrl.service.EQPTBaseService;
import com.deyuanyun.pic.ctrl.service.ImportService;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 模块描述
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/9/22）
 * @see // 引用的类
 * Method List:
 * 1.----------------
 * 2.----------------
 * History:
 */
public class OclpointValidata implements ValidateInterface {
    @Override
    public <T, E, C> boolean apply(T t, E e, C c) throws NoSuchFieldException {
        Object obj= (Object) c;
        DbTable table=obj.getClass().getAnnotation(DbTable.class);
        Field f=(Field) e;
        String val= (String) t;
        ImportService service = SpringAppliction.getBean(ImportService.class);

        try {
            String pipeLineId = BeanUtils.getSimpleProperty(c,"pipeLineId");
            Map<String,Object> oclpoint = service.queryOclpointByCustomNumber(pipeLineId,val);
            if(oclpoint != null )
            {

                BeanUtils.setProperty(c,f.getName(),oclpoint.get("ID_"));
                if(oclpoint.get("MILEAGE") != null)
                {
                    BeanUtils.setProperty(c,"mileage",""+oclpoint.get("MILEAGE"));
                }

                return true;
            }else{
                return false;
            }


        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (InvocationTargetException e1) {
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        }
        return false;
    }

    @Override
    public String getName() {
        return null;
    }
}
